<?php
namespace app\common\model;
use think\Model;

class Recommend extends CommonModel
{
    //主键
    protected $pk = 'recommend_id';
    public static $agentNext = [];
    public static $countTimes = 0;
    public static $agentLevel = [];
    /**
     * 我的推荐会员列表
     */
    public function myRecommend($memberId)
    {
        $recommendModel = model('common/Recommend');
        $memberModel = model('common/Members');
        $res = $recommendModel->fetchAll(['member_id' => $memberId]);
        foreach ($res as $value) {
            $memberInfo1 = $memberModel->fetch(['member_id' => $value['referee_id']]);
            if(!empty($memberInfo1)){
                self::$agentNext[] = $memberInfo1;
            }
            $res2 = $recommendModel->fetchAll(['member_id' => $value['referee_id']]);
            foreach ($res2 as  $value2) {
                $memberInfo2 = $memberModel->fetch(['member_id' => $value2['referee_id']]);
                if(!empty($memberInfo2)){
                    self::$agentNext[] = $memberInfo2;
                }
                $res3 = $recommendModel->fetchAll(['member_id' => $value2['referee_id']]);
                foreach ($res3 as  $value3) {
                     $memberInfo3 = $memberModel->fetch(['member_id' => $value3['referee_id']]);
                     if(!empty($memberInfo3)){
                         self::$agentNext[] = $memberInfo3;
                     }
                }
            }
        }
        return self::$agentNext;
    }


    /**
     * 获取业务员下的成员列表
     */
    public function getSellerMemberList($filter, $page, $limit, $order = 'create_time desc')
    {
        $membersModel      = model('common/Members');
        $memberWalletModel = model('common/MemberWallet');
        $competitionModel  = model('common/Competition');
        $res   = $this->where($filter)->limit($page, $limit)->order($order)->select();
        $res   = $res ? $res->toArray() : [];
        foreach ($res as &$value) {
            $refereeInfo = $membersModel->fetch(['member_id' => $value['referee_id']], 'username,phone,avatar,create_time');
            if(!empty($refereeInfo)){
                $value['username'] = $refereeInfo['username'];
                $value['phone']    = $refereeInfo['phone'];
                $value['phone']    = $refereeInfo['phone'];
                $value['create_time']   = $refereeInfo['create_time'];
            }else{
                $value['username'] = '';
                $value['phone']    = '';
                $value['avatar']   = '';
                $value['create_time']   = '';
            }

            //usdt余额
            $restUsdt = $memberWalletModel->fetch(['member_id' => $value['referee_id']], 'usdt');
            if(empty($restUsdt)){
                $value['usdt'] = 0;
            }else{
                $value['usdt'] = $restUsdt['usdt'];
            }

            //猜输、猜赢、总盈利、总流水
            $loseScore = $competitionModel->sumData(['member_id' => $value['referee_id'], 'result' => $competitionModel::TRANSPORT_STATUS, 'status' => $competitionModel::GUESSING_END_STATUS], 'score');
            //猜赢
            $winScore = $competitionModel->sumData(['member_id' => $value['referee_id'], 'result' => $competitionModel::WIN_STATUS, 'status' => $competitionModel::GUESSING_END_STATUS], 'score');
            //总流水
            $map = array(
                ['member_id', '=', $value['referee_id']],
                ['status', '<>', $competitionModel::CANCEl_STATUS]
            );
            $total = $competitionModel->sumData($map, 'amount');
            //总盈利
            $value['profit']     = $winScore - $loseScore;
            $value['lose_score'] = $loseScore;
            $value['win_score'] = $winScore;
            $value['total'] = $total;

        }

        $total = $this->where($filter)->count($this->pk);
        return ['list' => $res, 'total' => $total];
    }

    public  function getRecommendPeople($allMemberId)
    {
        return $this->where('referee_id','in',$allMemberId)->field('member_id,referee_id')->select()->toArray();
    }

    /*
     *  获取下级用户id
     */
    public  function  getSubordinateUser($where)
    {
        return $this->where($where)->field('member_id,referee_id')->select()->toArray();
    }
}